var Crawler = require('crawler');
var fs = require('fs');

fs.stat('./data', function (err, stats) {
    if (err) {
        // console.log(err);
        fs.mkdir('./data');
        console.log('创建文件夹成功')
    }
    else {
        // console.log(stats);
        console.log('文件夹已经存在')
    }
})

var c = new Crawler({
    maxConnections: 10
})

var videoList = [{
    url:'http://www.meipai.com/medias/hot?p=',
    name:'热门',
    code:'remen',
    pageCount:'1'
},{
    url:'http://www.meipai.com/square/62?p=',
    name:'音乐',
    code:'yinyue',
    pageCount:'1'
},{
    url:'http://www.meipai.com/square/13?p=',
    name:'搞笑',
    code:'gaoxiao',
    pageCount:'1'
},{
    url:'http://www.meipai.com/square/16?p=',
    name:'名人明星',
    code:'mingrenmingxing',
    pageCount:'1'
}
]

videoList.forEach(item=>{
    getVideos(item.code,item.url,item.pageCount);
})

function getVideos(type, url, pageCount) {
    getVideoData(url, 1, pageCount)
    var videos = [];
    function getVideoData(baseUrl, page, pageCount) {
        var url = baseUrl + page;
        c.queue([{
            uri: url,
            callback: function (error, res, done) {
                if (error) {
                    console.log(error);
                }
                else {
                    var $ = res.$;
                    // console.log($('title').text())
                    $('#mediasList li').each(function(){
                        var video = {};
                        video.title = $(this).find('.content-l-video a strong').text();
                        video.img = $(this).find('img').attr('src');
                        video.link = $(this).find('.content-l-video').attr('data-video');
                        videos.push(video);
                    })

                    if (page < pageCount) {
                        getVideoData(baseUrl, page + 1, pageCount)
                    }
                    else {
                        fs.writeFile(`./data/video_${type}.json`, JSON.stringify(videos));
                        console.log(`写入文件完成...${type}.json`)
                    }
                }
                done();
            }
        }])
    }

}
